package com.school.util;

import com.school.entity.User;
import com.school.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 数据清理工具类
 * 用于清理数据库中的残留数据
 * 仅在指定profile下运行
 */
@Component
@Profile("cleanup")
public class DataCleanupUtil implements CommandLineRunner {

    @Autowired
    private UserService userService;

    @Override
    public void run(String... args) throws Exception {
        System.out.println("开始清理残留数据...");
        
        // 查找所有学生用户
        List<User> studentUsers = userService.findByRole("student");
        System.out.println("找到 " + studentUsers.size() + " 个学生用户");
        
        // 打印学生用户信息
        for (User user : studentUsers) {
            System.out.println("ID: " + user.getId() + 
                              ", 用户名: " + user.getUsername() + 
                              ", 姓名: " + user.getName() + 
                              ", 学号: " + user.getStudentId());
        }
        
        // 删除指定的学生用户（学生1和学生2）
        for (User user : studentUsers) {
            if ("学生1".equals(user.getName()) || "学生2".equals(user.getName())) {
                System.out.println("删除用户: " + user.getName() + " (ID: " + user.getId() + ")");
                userService.deleteById(user.getId());
            }
        }
        
        System.out.println("数据清理完成");
    }
}
